package com.xsp.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsp.eduservice.entity.EduTeacher;
import com.xsp.eduservice.entity.vo.TeacherQuery;
import com.xsp.eduservice.service.EduTeacherService;
import com.xsp.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-15
 */
@Api(description = "讲师管理模块")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //所有讲师列表
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("item",list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R deleteById(@ApiParam(name = "id",value = "讲师id",required = true) @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        return flag?R.ok():R.error();
    }

    //讲师分页
    @ApiOperation(value = "讲师分页")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)@PathVariable Long limit){
        //创建page对象
        //page：当前页码  limit：每页记录数
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        //调用service的方法，传入page对象和条件
        eduTeacherService.page(teacherPage,null);
        //获取当前记录所有数据
        List<EduTeacher> records = teacherPage.getRecords();
        //获取总记录数
        long total = teacherPage.getTotal();

        //两种返回方式
//        Map map = new HashMap<>();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);
        //返回total和rows
        return R.ok().data("total",total).data("rows",records);
    }

    //条件分页
    //如果使用@RequestBody就需要使用@PostMapping
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable long page,
                                  @PathVariable long limit,
                                  //required = false 设置为非必须的参数
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //调用查询方法
        Page<EduTeacher> teacherPage = eduTeacherService.pageCourseCondition(page,limit,teacherQuery);
        //获取总记录数
        long total = teacherPage.getTotal();
        //获取拿到的全部数据
        List<EduTeacher> records = teacherPage.getRecords();
        //返回数据
        return R.ok().data("total",total).data("list",records);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        return save?R.ok():R.error();
    }

    //id查询讲师
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher)    ;
    }

    //id更新讲师
    @PutMapping("updateTeacher/{id}")
    public R updateTeacher(@PathVariable(required = true) String id
            ,@RequestBody(required = true) EduTeacher eduTeacher){
        eduTeacher.setId(id);
        eduTeacherService.updateById(eduTeacher);
        return R.ok();
    }

    @PostMapping("updateTeacher2")
    public R updateTeacher2(@RequestBody(required = true) EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        return update?R.ok():R.error();
    }

}

